import com.entity.Student;

import java.util.HashMap;

public class Main {


    public static void main(String[] args) {
        //String自己重写了equals和hashCode()，且是final class，
        //用它做hashmap的key可保证key每次的hashCode()相等
        HashMap<String,String> m2 = new HashMap<>();
        String str1 = new String("123");
        String str2 = new String("123");
        m2.put(str1,str1);
        System.out.println(m2.get(str2)); // print true

        //自定义的类做为key的话，则必须重写equals()和hashCode()
        Student s1 = new Student("young",20);
        Student s2 = new Student("young",20);
        HashMap<Student,Student> map = new HashMap<>();
        map.put(s1,s1);
        System.out.println(map.get(s2)); //若不重写equals()和hashCode()，就会返回false

    }
}
